param($RECEIVERS, $MSG_TITLE, $MSG_CONTENT)

#########################################################
# call demo:
#   .\zentaoIMNotify.ps1 "admin,luoqi" "LQ" "zzzzzzz123"
#########################################################

#
function strMD5($str) {
    $someString = $str
    $md5 = New-Object -TypeName System.Security.Cryptography.MD5CryptoServiceProvider
    $utf8 = New-Object -TypeName System.Text.UTF8Encoding
    $hash = [System.BitConverter]::ToString($md5.ComputeHash($utf8.GetBytes($someString))).replace('-','').ToLower()
    $hash
    
}


$ZENTAO_KEY = "e7e2ab74dc87a82da69070db26ad2a8a"
$APP_CODE = "kanqIM"

$time_now = (([DateTime]::Now.ToUniversalTime().Ticks - 621355968000000000)/10000).tostring().Substring(0,13)

$str = $APP_CODE + $ZENTAO_KEY + $time_now
$token = strMD5 $str
$url = "http://172.4.2.5:88/zentao/api.php?m=im&f=sendNotification&code=kanqIM&time={0}&token={1}" -f $time_now, $token;


Write-Host $url


$Body = @{
	title=$MSG_TITLE
	content=$MSG_CONTENT
	contentType='plain'
	users=$RECEIVERS -split ","
	sender=@{name='devopsRobot'; id='1'}
  }
  Invoke-WebRequest -Uri  $url -Method Post -Body ($Body | ConvertTo-Json) -ContentType "application/json"